package com.umeng.soexample.mvp.persenter;

import android.util.Base64;

import com.blankj.utilcode.util.EncryptUtils;
import com.umeng.lib_core.mvp.presenter.BasePresenter;
import com.umeng.soexample.bean.UserEntity;
import com.umeng.soexample.mvp.contract.UserLoginFragmentContract;

import org.json.JSONException;
import org.json.JSONObject;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class UserLoginFragmentPresenter extends BasePresenter<UserLoginFragmentContract.UserLoginIView, UserLoginFragmentContract.UserLoginIModel> {
    private String privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJa4C5IKvNRcLWXiLFcF4F+i1S2QAusCMszlQeJV84UetEkczjUdJ4dWbnpRkeAmXCTzRHyO67XKS6GSCuKVO/sf7cyll0i6e+d0MSWB2CTxojYingZSV6ZQO8K1Z3fJyFYSHiRhDwJ4idC80zTyKagsWV29uNa38iQYr4FwbNqZAgMBAAECgYAxV1k6W1eMMg0OsKeRabQVuwoNG3tJEnQtDdSu0zKg3vdohAyh6MR7EvmiA7g86HH8CsPd/y/9WJe/8j6sBO0Ye9gt7eyQ2NiwWvlTuwNmngcSTapVvVI6NEyJFMfQt9PB1EHLNAXlz8jtJUyA7C48jReQD9p/SzAP0VxG7lwyMQJBAOjE7hAZ/6fyP3DB1fG7jr9gONZcz3TUaqx6BUn4GKZnckW08ht9Xqcqft5Hthu8BbLM9ptQ0U8QZekrJwD6ya0CQQClwstZMPu8jLhsgugVwodcG1mPEOiw9Yjnmt9+WTI07Ll2uFv//hRXBnahBBnZbucUYEbUY3kqUX9b3e9TmEodAkEAybPMbxt4VDoxCy6Mi/pxChkBZ4/pHV3sSiU6bAyWn6vIc+sGWRfca5MBePA/N+1IKtY9Y/02QwL8rH5+P/URyQJAL/hdjORGFdzLimuf6pwvPBKWKncEQCHuisghIZmClBpl2duklELddAnkztg2+tvDd/wcw14+NGb9aoKhvhl2aQJAbvcgoPU+xs0CjeexH+TS2S/jKkTRpvP2CpPK/k71m13xWdE8RtMkYY1measRmlIwOfWze7ll/PGT4dxWf31FNg==";
    @Inject
    public UserLoginFragmentPresenter(UserLoginFragmentContract.UserLoginIView userLoginIView, UserLoginFragmentContract.UserLoginIModel userLoginIModel) {
        super(userLoginIView, userLoginIModel);
    }

    public void request(String name, String pawd){
        //rsa加密
        byte[]buff =  EncryptUtils.encryptRSA(pawd.getBytes(),
                Base64.decode(privateKey.getBytes(),Base64.DEFAULT),
                false,"RSA/ECB/PKCS1Padding");
        //base64
        String pwdStr = Base64.encodeToString(buff, Base64.DEFAULT);
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("username", name);
            jsonObject.put("password", pwdStr);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        m.login(jsonObject.toString()).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<UserEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        v.showDialog();
                    }

                    @Override
                    public void onNext(UserEntity entity) {
                        v.hideDialog();
                        v.success(entity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        v.hideDialog();
                        v.showMessage(e.toString());
                    }

                    @Override
                    public void onComplete() {
                        v.hideDialog();
                    }
                });
    }

    public void register(String name, String pawd){
        //rsa加密
        byte[]buff =  EncryptUtils.encryptRSA(pawd.getBytes(),
                Base64.decode(privateKey.getBytes(),Base64.DEFAULT),
                false,"RSA/ECB/PKCS1Padding");
        //base64
        String pwdStr = Base64.encodeToString(buff, Base64.DEFAULT);
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("username", name);
            jsonObject.put("password", pwdStr);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        m.register(jsonObject.toString()).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<UserEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        v.showDialog();
                        disposable.add(d);
                    }

                    @Override
                    public void onNext(UserEntity entity) {
                        v.hideDialog();
                        v.success(entity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        v.hideDialog();
                        v.showMessage(e.toString());
                    }

                    @Override
                    public void onComplete() {
                        v.hideDialog();
                    }
                });

    }

}
